﻿/*
 * Created by SharpDevelop.
 * User: Edward Wilde (http://blogs.charteris.com/edwardw)
 * Date: 12/07/2007
 * Time: 06:40
 */

using System;

namespace Wax
{
	/// <summary>
	/// This type of command performs actions against a specific <see cref="PropertyType">property</see> of an <see cref="WatiN.Core.Element">element</see>.
	/// </summary>
	public abstract class PropertyCommand : Command
	{
		#region Private fields
		
		/// <summary>
		/// MethodType of property being validated.
		/// </summary>
		private PropertyType propertyToValidate;

        /// <summary>
        /// The name of the associated property.
        /// </summary>
	    private string propertyName;

        /// <summary>
        /// The attribute name
        /// </summary>
	    private string attributeName;

	    #endregion
		
		#region Constructors

	    #endregion
		
		#region Public properties
		
		/// <summary>
		/// MethodType of property being validated.
		/// </summary>
		public PropertyType PropertyType
		{
			get 
			{
				if (this.propertyToValidate == PropertyType.None)
				{
					this.propertyToValidate = CommandFactory.ParsePropertyType(this.PropertyName);
				}
				
				return this.propertyToValidate;
			}
		}
		
		/// <summary>
		/// Name of the property to validate.
		/// </summary>
		public string PropertyName
		{
			get
			{
                if (this.propertyName == null)
                {
                    this.propertyName = this.Arguments[this.Arguments.Count - 1];
                }

			    return this.propertyName;
			}
		}

        /// <summary>
        /// Property types <see cref="Wax.PropertyType.Attribute"/> in addition to having a property name,
        /// have attribute names. For example the property name might be Attrbiute:Class and the 
        /// attribute name would be Class
        /// </summary>
        /// <value>The name of the attribute.</value>
	    public string AttributeName
	    {
	        get
	        {
	            if (this.attributeName == null)
	            {
	                this.attributeName = this.PropertyName;

                    if (this.attributeName.Contains(":"))
                    {
                        this.attributeName = this.attributeName.Substring(this.attributeName.IndexOf(":", StringComparison.OrdinalIgnoreCase) + 1);

                        // WatiN / mshtml uses attribute names from the DOM so swap over the common ones here.
                        if (this.attributeName.Equals("class", StringComparison.OrdinalIgnoreCase))
                        {
                            this.attributeName = "ClassName";
                        }
                    }
	            }

	            return this.attributeName;
	        }
	    }
		
		#endregion
	}
}
